define(["jquery", 
    "PDUtilDir/util",
    "PDUtilDir/grid",
    "PDUtilDir/datagrid",
    "PDUtilDir/dialog",
    "PDUtilDir/slidebar",
    "PDUtilDir/tool",
    "Date","DateCN","css!DateCss"], function($,Util,Grid,DataGrid,Dialog,Slidebar,Tool) {

    var dialog ={
        id:"BaseDialog",
        cache:false,                 //是否缓存，默认为true
        title:"提示",
        width:"400px",
        height:"100px",
        dialogSize:"",               //modal-lg或modal-sm
        body:"nothing"
    };

    var showVerifyForm = function(){
        $.ajax({
            url:getServer()+"/sword/P2PData/getSchedulerInfo",
            success:function(result){

                $("#jobStatus").val(result.jobStatus);
                $("#firstExecuteTime").val(result.firstExecuteTime);
                $("#lastExecuteTime").val(result.lastExecuteTime);
                $("#finishedTime").val(result.finishedTime);
                $("#executeCounts").val(result.executeCounts);
                $("#completedSuccessCounts").val(result.completedSuccessCounts);

                $("#jobStatus").attr("disabled","disabled");
                $("#firstExecuteTime").attr("disabled","disabled");
                $("#lastExecuteTime").attr("disabled","disabled");
                $("#finishedTime").attr("disabled","disabled");
                $("#executeCounts").attr("disabled","disabled");
                $("#completedSuccessCounts").attr("disabled","disabled");
            }
        })
    }

    var saveVerifyRule = function(){
        $("#cronRule").validate({
            rules:{
                second:{
                    required:true
                },
                minute:{
                    required:true
                },
                hour:{
                    required:true
                },
                day:{
                    required:true
                },
                month:{
                    required:true
                },
                week:{
                    required:true
                },
                year:{
                    required:true
                }
            },
            messages:{
                second:{
                    required:"input please"
                },
                minute:{
                    required:"input please"
                },
                hour:{
                    required:"input please"
                },
                day:{
                    required:"input please"
                },
                month:{
                    required:"input please"
                },
                week:{
                    required:"input please"
                },
                year:{
                    required:"input please"
                }
            }
        });

        if($("#cronRule").valid()){

            var triggerParams = Tool.serialize("cronRule");

            $.ajax({
                url:getServer()+'/sword/P2PData/setReportScheduler',
                data:{
                    triggerParams:triggerParams
                },
                success:function(result){
                    if(result){
                        showVerifyForm();
                        $.extend(dialog,{body:"设置成功!"});
                    }else{
                        $.extend(dialog,{body:"设置失败"})
                    }
                    Dialog(dialog);
                }
            })
        }
    }

    var loadButton = function(){

        $("#btnSave").on("click",function(){
            saveVerifyRule();
        })

        $("#btnPause").on("click",function(){
            $.ajax({
                url:getServer()+"/sword/P2PData/pauseSchedulerJob",
                success:function(result){
                    if(result){
                        Util.alert("停止成功");
                    }else{
                        Util.alert("停止失败");
                    }
                }
            })
        })
    }

    var mainInit = function(){
        showVerifyForm();
        loadButton();
        window.setInterval(showVerifyForm,60*1000);
    };

    return {
        mainInit:mainInit,
    }
})
